package com.bookhouse.service.impl;

import com.bookhouse.dao.BaseDao;
import com.bookhouse.dao.MenuDao;
import com.bookhouse.entity.Menu;
import com.bookhouse.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by ciuscheung on 2016/4/21.
 */
@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {
    @Resource(name = "menuDao")
    private MenuDao menuDao;

    @Override
    protected BaseDao<Menu> getDao() {
        return this.menuDao;
    }

    @Override
    public Set<Menu> getAllMenu() {
        Set<Menu> sets = new HashSet<Menu>();
        Menu menu = this.get(Menu.class, 0);
        Set<Menu> levels1 = menu.getChildrenMenu();
        for (Menu level1 : levels1) {
            sets.add(level1);
            level1.setMenuSet(new HashSet<Menu>());
            Set<Menu> levels2 = level1.getChildrenMenu();
            for (Menu level2 : levels2) {
                level1.getMenuSet().add(level2);
            }
        }
        return sets;
    }
}